<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <form id="form">
        <input type="text" name="name">
        <input type="text" name="age">
        <input type="text" name="sex">
        <button id="btn">提交</button>
    </form>
    <script>
        const Form = document.getElementById('form')
        const Name = document.querySelector('#form input[name=name]');
        const Age = document.querySelector('#form input[name=age]');
        const Sex = document.querySelector('#form input[name=sex]');
        const Btn = document.getElementById('btn')

        Form.onsubmit = function () {
            const nameValue = Name.value
            const ageValue = Age.value
            const sexValue = Sex.value

            let xhr = new XMLHttpRequest()
            xhr.open('POST', '/adduser?a=1', true);
            xhr.setRequestHeader("Content-Type", "application/json;charset=utf-8")
            xhr.send(JSON.stringify({
                name: nameValue,
                age: ageValue,
                sex: sexValue
            }))
            
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status <= 299)) {
                    console.log(xhr.responseText);
                }
            }

            return false
        }
    </script>
</body>

</html>